package org.magento.pages;

import org.magento.data.CustomerData;
import org.magento.utils.ConfigProperties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

//This class represents /customer/account/login/ page

public class LoginPageFrontEnd extends Page {
	
	//Locators: 
	@FindBy(id= "email")
	public WebElement customerEmail;
	
	@FindBy(id = "pass")
	public WebElement customerPassword;
	

	@FindBy (id = "send2")
	public WebElement loginButton;
	
	@FindBy (linkText = "Forgot Your Password?")
	public WebElement forgotYourPasswordLink;
	
	@FindBy (xpath = "/html/body/div/div/div[3]/div/div/div/form/div[2]/div/div/button")
	public WebElement createAccountButton;
	
	
	//Actions 
	public MyDashboard loginAs(CustomerData customer){
		type(customerEmail, customer.email);
		type(customerPassword, customer.password);
		loginButton.click();
		return PageFactory.initElements(driver, MyDashboard.class);		
	}
	
	public LoginPageFrontEnd loginAsNot (CustomerData customer){
		type(customerEmail, customer.email);
		type(customerPassword, customer.email);
		loginButton.click();
		return PageFactory.initElements(driver, LoginPageFrontEnd.class);
	}
	
	public CreateCustomerAccount createAccount() {
		createAccountButton.click();
		return PageFactory.initElements(driver, CreateCustomerAccount.class);
	}
	
	public ForgotCustomerPassword forgotCustomerPassword(){
		forgotYourPasswordLink.click();
		return PageFactory.initElements(driver, ForgotCustomerPassword.class);		
	}
	
	//Assertions
	public boolean isCustomerLoginErrorDisplayed() {
		 return  driver.findElement(By.cssSelector("BODY")).getText().contains("Invalid login or password."); 
	}

	public boolean isPasswordReset() {
		 return  driver.findElement(By.cssSelector("BODY")).getText()
				 .contains("If there is an account associated with denis.ulyanov@iceshop.nl you will receive an email with a link to reset your password."); 
	}
	
	//Auto - generated: 
	public LoginPageFrontEnd(WebDriver driver) {
		super(driver);
	}

	@Override
		public void open() {
			driver.get(ConfigProperties.getProperty("url.customerLogin"));	
		}
	}


 